package org.spring.springboot.app.sys.area.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.sys.area.dao.SysAreaDao;
import org.spring.springboot.app.sys.area.domain.po.SysAreaPO;
import org.spring.springboot.app.sys.area.domain.vo.*;
import org.spring.springboot.app.sys.area.service.ISysAreaService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysAreaServiceImpl implements ISysAreaService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysAreaServiceImpl.class);

    @Autowired
    private SysAreaDao sysAreaDao;
    @Override
    public List<SysAreaPO> findAll() {
        return sysAreaDao.findPage(1,Integer.MAX_VALUE);
    }

    @Override
    public SysAreaPO find(String id) {
        return sysAreaDao.selectByPrimaryKey(id);
    }

    @Override
    public List<SysAreaPO> findPage(int pageNum, int pageSize) {
        return sysAreaDao.findPage(pageNum,pageSize);
    }

    @Override
    public int save(SysAreaPO po) {
        po.preInsert();
        return sysAreaDao.insertSelective(po);
    }

    @Override
    public int update(SysAreaPO po) {
        po.preUpdate();
        return sysAreaDao.updateByPrimaryKeySelective(po);
    }
    @Override
    public R saveArea(SysAreaSaveInVO vo) {
        R result = new R();
        SysAreaPO  po =  new SysAreaPO();
        po.preInsert();
        //查询上级
        SysAreaPO parent = find(vo.getParentId());
        int parentType =Integer.parseInt(parent.getType());
        if (parentType == 5) {//街道不能再添加下级
            result.setType(Type.OperError);
            result.setMessage("街道不能再添加下级");
            return result;
        }
        long parentCode =Long.parseLong(parent.getCode());
        //查询上级下的所有节点
        List<SysAreaVO> childList = findByParent(vo.getParentId());
        Long sort = 1L;
        long code = 0;
        if (childList != null) {
            for (SysAreaVO childVo :childList){
                if (sort <= childVo.getSort()) {
                    sort = childVo.getSort() + 1;
                }
                if (code <= Long.parseLong(childVo.getCode())) {
                    code = Long.parseLong(childVo.getCode());
                }
            }
        }
        if (vo.getSort() != null && vo.getSort() > 0) {
            sort = vo.getSort();
        }
        //需要计算
        switch (parentType) {
            case 1: {//顶级
                code = code > 0 ? code + 10000 : parentCode+10000;
            }
            break;
            case 2: {//省级
                code = code > 0 ? code + 100 : parentCode+100;
            }
            break;
            case 3: {//市级
                code = code > 0 ? code + 1 : parentCode+1;
            }
            break;
            case 4: {//区级
                code = code > 0 ? code + 1 : (parentCode*1000)+1;
            }
            break;
        }

        po.setParentId(vo.getParentId());
        po.setParentIds(parent.getParentIds()+po.getId()+",");
        po.setName(vo.getName());
        po.setSort(sort);
        po.setCode(code+"");
        po.setType((parentType+1)+"");
        int ret = sysAreaDao.insertSelective(po);
        if (ret == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }

    @Override
    public R updateArea(SysAreaUpdateInVO vo) {
        R result = new R();
        SysAreaPO  po =  find(vo.getId());
        po.preUpdate();
        if (StringUtils.isNotBlank(vo.getName())) {
            po.setName(vo.getName());
        }
        if (vo.getSort() != null && vo.getSort() > 0) {
            po.setSort(vo.getSort());
        }
        int ret =sysAreaDao.updateByPrimaryKeySelective(po);
        if (ret == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }

    @Override
    public List<SysAreaQueryMiniVO> findAllMini(SysAreaQueryMiniSearchVO searchVO) {
        return sysAreaDao.findAllMini(searchVO);
    }

    @Override
    public int delete(String id) {
        SysAreaPO sysAreaPO = new SysAreaPO();
        sysAreaPO.preDelete();
        sysAreaPO.setId(id);
        return sysAreaDao.updateByPrimaryKeySelective(sysAreaPO);
    }


    @Override
    public List<SysAreaVO> findByParent(String parentId) {
        return sysAreaDao.findByParent(parentId);
    }

}
